package lg.minecraft.plugin.pokemonmenu.config.builder;

import com.cobblemon.mod.common.item.PokemonItem;
import com.cobblemon.mod.common.pokemon.Pokemon;
import lg.minecraft.plugin.cobblemoncore.util.BukkitNmsConverter;
import lg.minecraft.plugin.cobblemoncore.util.PokemonPlaceholderReplacer;
import lg.minecraft.plugin.pokemonmenu.util.PokemonNBTUtil;
import net.minecraft.class_1799;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.List;
import java.util.Map;

/**
 * @author langle__
 * @version 1.0
 */
public class PokemonNBTItemBuilder {

    private final String name;
    private final List<String> lore;

    public PokemonNBTItemBuilder(String name, List<String> lore) {
        this.name = name;
        this.lore = lore;
    }

    public ItemStack build(Pokemon pokemon) {
        class_1799 pokemonItem = PokemonItem.from(pokemon);

        PokemonNBTUtil.writePokemonToNBT(pokemon, pokemonItem);

        ItemStack stack = BukkitNmsConverter.toItemStack(pokemonItem);

        Map<String, String> map = PokemonPlaceholderReplacer.buildPokemonInfoPlaceholders(pokemon);

        ItemMeta meta = stack.getItemMeta();
        if (meta != null) {
            meta.setDisplayName(PokemonPlaceholderReplacer.replacePlaceholders(name, map));
            meta.setLore(PokemonPlaceholderReplacer.replacePlaceholders(lore, map));
            stack.setItemMeta(meta);
        }
        return stack;
    }

}
